package qujian;

import java.util.ArrayList;
import java.util.List;

/**
 * 输入：nums = [0,1,2,4,5,7]
 * 输出：["0->2","4->5","7"]
 * 解释：区间范围是：
 * [0,2] --> "0->2"
 * [4,5] --> "4->5"
 * [7,7] --> "7"
 *
 * @author luolidong
 * @version 1.0
 * @create 2021-08-03 16:21
 */
public class L228 {
    public List<String> summaryRanges(int[] nums) {
        List<String> list = new ArrayList<>();
        int left = 0, right = 0;
        while (right < nums.length) {

            if (right < nums.length - 1 && nums[right] + 1 == nums[right + 1]) {
                right++;
            } else {
                if (left == right) {
                    list.add(nums[left] + "");
                } else {
                    list.add(nums[left] + "->" + nums[right]);
                }
                right++;

                left = right;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        L228 l228 = new L228();
        List<String> list = l228.summaryRanges(new int[]{0, 1, 2, 4, 5, 7, 9});
        System.out.println(list);
    }
}
